<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<title>权限管理系统</title>
<jsp:include page="/views/include.jsp"></jsp:include>
<script src="<c:url value="/js/fileUpload/ajaxfileupload.js"/>"></script>
<script type="text/javascript">
        	var contextPath = '<%=request.getContextPath()%>';
        	$.parser.onComplete = function(){
            	$('body').css('visibility','visible');
            	setTimeout(function(){
	            	$('#loading-mask').remove();
            	},50);
        	};
        	$(function(){
            	$(window).resize(function(){
                	$('#mainlayout').layout('resize');
            	});
            	
            	$('#t-users').datagrid({onDblClickRow:function dblClickRow(rowIndex,rowData){
    				$('#myform').formid('loadit',rowData);
    				$.each($('#myform input'),function(i){
    					$(this).attr("readonly","true");
    				});
    				$("#dlg-buttons a::first-child").hide();
    				$('#dlg').dialog('setTitle','查看用户信息').dialog('open');
    			}});
        		$('#qcompanyId').combotree({
					url:'<c:url value="/permissions/getTreeCompany.tg"/>',
					valueField:'id',
					textField:'text',					
					onLoadSuccess: function (row, data) { 
						 $('#qcompanyId').combotree('tree').tree("collapseAll");
					}
				});
        	});
        </script>
<style type="text/css">
.e-input {
	width: 198px;
	border: 1px solid #A4BED4;
	height: 18px;
	line-height: 18px;
}
</style>
<script type="text/javascript">
//----------------------------------------------用来格式化显示状态，是否打开和是否叶子
var status1;
$.getJSON("<c:url value='/permissions/outDicJsonByNicknameActions.tg?nickName=status'/>", function(json){
	status1=json;
});
function statusFormatter(value){
	for(var i=0; i<status1.length; i++){		
		if (status1[i].value == value) return status1[i].name;
	}
	return value;
}
var yesornoJson;
$.getJSON("<c:url value='/permissions/outDicJsonByNicknameActions.tg?nickName=yesorno'/>", function(json){
	yesornoJson=json;
});
function yesornoFormatter(value){
	for(var i=0; i<yesornoJson.length; i++){		
		if (yesornoJson[i].value == value) return yesornoJson[i].name;
	}
	return value;
}
var sex;
$.getJSON("<c:url value='/permissions/outDicJsonByNicknameActions.tg?nickName=sex'/>", function(json){
	sex=json;
});
function sexFormatter(value){
	for(var i=0; i<sex.length; i++){
		if (sex[i].value == value) return sex[i].name;
	}
	return value;
}
var usertype;
$.getJSON("<c:url value='/permissions/outDicJsonByNicknameActions.tg?nickName=usertype'/>", function(json){
	usertype=json;
});
function usertypeFormatter(value){
	for(var i=0; i<usertype.length; i++){
		if (usertype[i].value == value) return usertype[i].name;
	}
	return value;
}
function isNumberOr_Letter( s ){//判断是否是数字或字母 
	var regu = "^[0-9a-zA-Z\_]+$"; 
	var re = new RegExp(regu); 
	if (re.test(s)) { 
		return true; 
	}else{ 
		return false; 
	} 
}
//---------------------------------------------------------------------

		var actionUrl;
		function newItem(){
			$('#myform').form('clear');
			$.each($('#myform input'),function(i){
				$(this).removeAttr("readonly");
			});
			document.getElementById("chkResult").innerHTML='';
			$("#dlg-buttons a::first-child").show();
			
			//$('#parentId').combotree({
			//	url:'<c:url value="/permissions/getTreeGroup.tg"/>',
			//	valueField:'id',
			//	textField:'text'				
			//});
			$('#deptId').combotree({
				url:'<c:url value="/permissions/getTreeDepartment.tg"/>',
				valueField:'id',
				textField:'text',
				onLoadSuccess: function (row, data) { 
					 $('#deptId').combotree('tree').tree("collapseAll");
				}
			});
			$('#companyId').combotree({
				url:'<c:url value="/permissions/getTreeCompany.tg"/>',
				valueField:'id',
				textField:'text'
			});	
			
			$('#usertype').combobox('setValue','2');
			$('#isSa').combobox('setValue','0');
			document.getElementById('upUserImage').style.visibility = "hidden";
			$('#dlg').dialog('setTitle', '填写用户资料').dialog('open');
			actionUrl = '<c:url value="/permissions/saveUser.tg"/>';
		}
		function saveItem(){
			//校验输入			
			if(!isNumberOr_Letter($('#logonid').val())){
				alert('登录名必须为字母和数字！');
				return false;
			}
			//如果用户修改密码
			if($('#newPassword').val()!='123456'){
				if(!checkpwd($('#newPassword').val())){
					alert('密码长度必须大于6位小于20位,至少包含字母和数字！');
					return false;
				}
			}else{
				$('#newPassword').val('');
			}
				
			$('#myform').form('submit', {
				url:actionUrl,
				onSubmit: function(){
					return $('#myform').form('validate');
				},
				success: function(data){
					$('#dlg').dialog('close');
					$('#t-users').datagrid('reload');
					data=eval('('+data+')');
					if(data.success){
						$.messager.show(
							{
								title:'提示',
								msg:'操作成功！',
								showType:'slide'
							}
						);
					}
					if(data.error){
						$.messager.alert('提示','操作失败！','error');
					}
				}
			});
		}
		
		function editItem(){
			$('#myform').form('clear');
			var row = $('#t-users').datagrid('getSelected');
			if (row){
				if(row.usertype=='1'||row.usertype=='内置用户'){
					alert('请选择非内置用户修改');
					return;
				}
				$('#userId').val(row.id);
				document.getElementById("chkResult").innerHTML='';
				actionUrl = '<c:url value="/permissions/findUser.tg"/>'; 
				$('#userform').formid('loadit',row);
				
				$('#userform').form('submit', {
					url:actionUrl,
					onSubmit: function(){
						return true;
					},
					success: function(data){
						data=eval('('+data+')');
						var tempObj = data.rows[0];

						if(data.parentid!='')
						{
							$('#parentId').val(data.parentid);
						}
							
						$('#deptId').combotree({
							url:'<c:url value="/permissions/getTreeDepartment.tg"/>',
							valueField:'id',
							textField:'text'				
						});
						//$('#deptId').combotree('setText',tempObj.deptName);
						$('#deptId').combotree('setValue',tempObj.deptId);
						
						$('#companyId').combotree({
							url:'<c:url value="/permissions/getTreeCompany.tg"/>',
							valueField:'id',
							textField:'text'
						});
						$('#companyId').combotree('setText',tempObj.companyName);
						$('#companyId').combotree('setValue',tempObj.companyId);
						
						$('#employeddate').datebox('setValue',tempObj.employeddate);
						$('#birthday').datebox('setValue',tempObj.birthday);
						$('#lastlogondate').datebox('setValue',tempObj.lastlogondate);
						$('#lastlogoffdate').datebox('setValue',tempObj.lastlogoffdate);
						$('#newPassword').val('123456');  //赋予一个常量值,保存的时候如果没有修改，则改回原值ext7
						if(!(tempObj.photoPath==null||tempObj.photoPath==''))
							document.getElementById('userImage').src=tempObj.photoPath;
						else
							document.getElementById('userImage').src="/autocrm/userPhoto/default.jpg";
						$('#photoPath').val(tempObj.photoPath);
						$('#logonid').attr("readonly","true");
						
					}
				});
				document.getElementById('upUserImage').style.visibility = "visible";
				$('#dlg').dialog('setTitle', '修改系统资料').dialog('open');
				$.each($('#myform input'),function(i){
					$(this).removeAttr("readonly");
				});
				$("#dlg-buttons a::first-child").show();
				actionUrl = '<c:url value="/permissions/updateUser.tg"/>';
			}else{
				alert('请选择用户修改');
			}
		}
		function delItem(){
			var rows = $('#t-users').datagrid('getSelections');
			if(rows){
				if(confirm('确认要删除用户?'))
				{	
					var ids = "";
					for(var i=0; i<rows.length; i++){
						if(!(rows[i].usertype=='1'||rows[i].usertype=='内置用户')){
							ids = ids + ','+rows[i].id;
						}
					}
					if(ids.length==0)
					{
						alert('请选择非内置用户进行删除！');
						return;
					}
					$('#userId').val(ids);
					actionUrl = '<c:url value="/permissions/delUser.tg"/>';
					$('#userform').form('submit', {
						url:actionUrl,
						success: function(data){
							$('#dlg').dialog('close');
							$('#t-users').datagrid('reload');
							data=eval('('+data+')');
							if(data.success){
								$.messager.show(
									{
										title:'提示',
										msg:'操作成功！',
										showType:'slide'
									}
								);
							}
							if(data.error){
								$.messager.alert('提示','操作失败！','error');
							}
						}
					});
				}
			}else{
				alert('请选择要删除的用户！谢谢'); 
			}
		}
		
		function searchUsers(){			
			var param = {};
			param['user.logonid'] = $('#qloginid').val();
			param['user.name'] = $('#quserName').val();
			
			param['user.companyId'] = $('#qcompanyId').combotree('getValue');
			param['user.status'] = $('#qstatus').combobox('getValue');	
			
			
			$('#t-users').datagrid("load",param);
			//$('#t-users').datagrid('options').url = url;
		}

	 function chkpwd(obj){
		  var t=obj.value;
		  var id=getResult(t);
		  
		//定义对应的消息提示
		  var msg=new Array(4);
		  msg[0]="密码过短。";
		  msg[1]="密码安全性差。";
		  msg[2]="密码强度良好。";
		  msg[3]="密码强度很高。";
		  
		  var sty=new Array(4);
		  sty[0]=-45;
		  sty[1]=-30;
		  sty[2]=-15;
		  sty[3]=0;
		  
		  var col=new Array(4);
		  col[0]="gray";
		  col[1]="red";
		  col[2]="#ff6600";
		  col[3]="Green";
		  
		  var sWidth=200;
		  var sHeight=15;
		  var Bobj=document.getElementById("chkResult"); 
		
		  Bobj.style.fontSize="12px";
		  Bobj.style.color=col[id];
		  Bobj.style.width=sWidth + "px";
		  Bobj.style.height=sHeight + "px";
		  Bobj.style.lineHeight=sHeight + "px";
		  Bobj.style.textIndent="20px";
		  Bobj.innerHTML="检测提示：" + msg[id];
	 }
	 
	 function getResult(s){
		  if(s.length < 6){
		   return 0;
		  }
		  var ls = 0;
		  if (s.match(/[a-z]/ig)){
		   ls++;
		  }
		  if (s.match(/[0-9]/ig)){
		   ls++;
		  }
		   if (s.match(/(.[^a-z0-9])/ig)){
		   ls++;
		  }
		  return ls;
	 }
	 function checkpwd(s)
	 {
		 if(s.length < 6){
			return false;
		 }
		 if(s.length > 20){
			return false;
		  }		  
		  var ls = 0;
		  if (s.match(/[a-z]/ig)){
		   ls++;
		  }
		  if (s.match(/[0-9]/ig)){
		   ls++;
		  }
		  if(ls==2){
			  return true;
		  }else{
			  return false;
		  }
	 }
		function ajaxFileUpload()
	    {		
	        $.ajaxFileUpload
	        (
	            {
	                url:'<c:url value="/permissions/uploadFileUser.tg"/>?id='+$('#userId').val(), 
	                secureuri:false,
	                fileElementId:'upload',
	                dataType: 'json',
	                success: function (data, status)
	                {
	                    //alert(data);
	                    data=eval( "data = " + data );

	                    var address=window.location.href;
	                    var endIdx = address.indexOf('/',10);
	                    var result = address.substring(0, endIdx)+data.url;
	        	        $("#photoPath").val(result);

	        	        document.getElementById('userImage').src=result;	                    
	                    
	                },
	                error: function (data, status, e)
	                {
	                    alert(e);
	                }
	            }
	        )
	        
	        return false;

	    } 
	 
 </script>